package com.chenqi.community.service.Impl;

import com.chenqi.community.dao.TagDao;
import com.chenqi.community.entity.Tag;
import com.chenqi.community.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * @author Ardai
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class})
public class TagServiceImpl implements TagService {

    @Autowired
    private TagDao tagDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Tag> findAllTags() {
        return tagDao.findAllTags();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void addTags(Integer pid, Integer tid) {
        tagDao.insertTags(pid,tid);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Tag> findTagsByPostId(Integer pid) {
        List<Integer> tagsId = tagDao.findTagsByPostId(pid);
        //根据标签的id去查询标签并封装到List集合里面
        List<Tag> tags = new ArrayList<>();
        for (Integer id : tagsId) {
            Tag tag = tagDao.findTagById(id);
            tags.add(tag);
        }
        return tags;
    }




}
